package com.patrol.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.patrol.dao.TaskPointDao;
import com.patrol.dao.WorkDao;
import com.patrol.pojo.TaskPoint;
import com.patrol.pojo.UserTask;
import com.patrol.service.TaskPointService;
import com.patrol.service.UserService;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class TaskPointServiceImpl extends ServiceImpl<TaskPointDao, TaskPoint> implements TaskPointService {
    @Resource
    private UserService userService;

    @Resource
    private TaskPointDao taskPointDao;

    @Resource
    private WorkDao workDao;

    @Override
    public TaskPoint selectPointById(Integer taskId) {
        TaskPoint taskPoint = baseMapper.selectById(taskId);
        return taskPoint;
    }

    @Override
    public List<TaskPoint> selectAllPoint() {
        List<TaskPoint> list = baseMapper.selectList(null);
        return list;
    }

    @Override
    public boolean clockPoint(int taskId, int userId) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        taskPointDao.finishPoint(taskId, userId, df.format(new Date()));
        //修改打卡频次
        TaskPoint point = baseMapper.selectById(taskId);
        if (null == point){
            return false;
        }
        int frequency = point.getFrequency();
        point.setFrequency(++frequency);
        baseMapper.updateById(point);
        //巡更人员未完成的任务点数量减一
        int rwId =  workDao.selectNowUserRWId(userId);
        workDao.updateNotNumByRWId(rwId);
        return true;
    }

    @Override
    public TaskPoint selectPointByName(String taskName) {
        TaskPoint taskPoint = taskPointDao.selectPointByName(taskName);
        return taskPoint;
    }

    @Override
    public void allotPoint(String user, String point) {
        int u = Integer.parseInt(user);
        int p = Integer.parseInt(point);
        taskPointDao.allotPoint(u,p);
    }

    @Override
    public List<Integer> findPointIds(int userId) {
        return taskPointDao.findPointIds(userId);
    }

    @Override
    public void changePointState(String taskId, String date) {
        taskPointDao.changePointState(Integer.parseInt(taskId), date);
    }

    @Override
    public boolean delUserTask(String userId, String taskId) {
        int i = taskPointDao.delUserTask(Integer.parseInt(userId), Integer.parseInt(taskId));
        return i==1;
    }

    @Override
    public int selectTaskState(int taskId,int userId) {
        return taskPointDao.selectTaskState(taskId, userId);
    }

    @Override
    public boolean selectUserPoint(String userId,String taskId) {
        Integer i = taskPointDao.selectUserPoint(Integer.parseInt(userId), Integer.parseInt(taskId));
        if (i==null){
            return true;
        }
        return false;
    }

    @Override
    public int selectUserRWPointState(Integer userId, Integer taskId) {
        int i = taskPointDao.selectRWPointState(userId, taskId);
        return i;
    }

    @Override
    public void updateTaskPointSign(String userId) {
        taskPointDao.updatePointSign(userId);
    }
}
